package com.hardware.controller.system;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hardware.common.R;
import com.hardware.dto.RoleDto;
import com.hardware.entity.system.Module;
import com.hardware.entity.system.Role;
import com.hardware.entity.system.RoleModule;
import com.hardware.service.system.ModuleService;
import com.hardware.service.system.RoleModuleService;
import com.hardware.service.system.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.List;
import java.util.stream.Collectors;

import static com.hardware.utils.RedisConstants.CACHE_CATEGORY_SHOP_KEY;
import static com.hardware.utils.RedisConstants.CACHE_SUPPLIER_KEY;

@Slf4j
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;
    @Autowired
    private RoleModuleService roleModuleService;

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
//    @Cacheable(value = CACHE_CATEGORY_SHOP_KEY,key = "#page +'_'+ #pageSize")
    public R<Page> page(int page, int pageSize){

        return roleService.page(page,pageSize);
    }

    /**
     * 根据id删除分类
     * @param id
     * @return
     */
    @DeleteMapping
//    @CacheEvict(value = CACHE_CATEGORY_SHOP_KEY, allEntries = true)
    public R<String> delete(Long id){

        roleService.delete(id);

        return R.success("分类信息删除成功");
    }

    /**
     * 新增分类
     * @param role
     * @return
     */
    @PostMapping
//    @CacheEvict(value = CACHE_CATEGORY_SHOP_KEY, allEntries = true)
    public R<String> save(@RequestBody Role role){
        roleService.save(role);
        return R.success("新增分类成功");
    }


    /**
     * 根据id修改分类信息
     * @param role
     * @return
     */
    @PutMapping
//    @CacheEvict(value = CACHE_CATEGORY_SHOP_KEY, allEntries = true)
    public R<String> update(@RequestBody Role role){

        roleService.updateById(role);

        return R.success("修改分类信息成功");
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
//    @Cacheable(value = CACHE_SUPPLIER_KEY,key = "#id")
    public R<RoleDto> get(@PathVariable Long id){
        Role role = roleService.getById(id);
        RoleDto roleDto = new RoleDto();
        BeanUtil.copyProperties(role,roleDto);

        //根据ID 查询该角色下的权限
        LambdaQueryWrapper<RoleModule> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RoleModule::getRoleId,id);

        List<RoleModule> list = roleModuleService.list(queryWrapper);
        List<Long> collect = list.stream().map((itme) -> {
            Long moduleId = itme.getModuleId();
            return moduleId;
        }).collect(Collectors.toList());

        roleDto.setModuleIds(collect);

        return R.success(roleDto);
    }

    @PutMapping("/update")
    public R<String> setModules(@RequestBody RoleDto roleDto){
        //先查询原有权
        LambdaQueryWrapper<RoleModule> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RoleModule::getRoleId,roleDto.getId());
        //删除原有权限
        roleModuleService.remove(queryWrapper);
        //新增权限
        List<RoleModule> collect = roleDto.getModuleIds().stream().map((itme) -> {
            RoleModule roleModule = new RoleModule();
            roleModule.setModuleId(itme);
            roleModule.setRoleId(roleDto.getId());
            return roleModule;
        }).collect(Collectors.toList());
        roleModuleService.saveBatch(collect);
        return R.success("更新成功");
    }

    @GetMapping("/author")
    public R<List<Role>> author(){
        List<Role> list = roleService.list();
        return R.success(list);
    }
}
